package cn.com.blueInfo.business.ddns.service.impl;

import cn.com.blueInfo.business.ddns.entity.DDNSParams;
import cn.com.blueInfo.business.ddns.service.DDNSService;
import cn.com.blueInfo.utils.client.HttpClient;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Log4j2
@Service
public class DDNSServiceImpl implements DDNSService {

    @Autowired
    private DDNSParams ddnsParams;

    @Override
    public void startDDNSService() {
        // DDNS服务开启时执行
        if (ddnsParams.isEnabled()) {
            if ("aliyun".equals(ddnsParams.getService())) {
                aliYun();
            } else if ("tencent".equals(ddnsParams.getService())) {
                tencent();
            } else {
                log.error("服务平台参数错误");
            }
        }
    }

    private void aliYun() {
        DDNSParams.AliYun aliYunParam = ddnsParams.getAliyun();
        String accessKeyId = aliYunParam.getAccessKeyId();
        String accessKeySecret = aliYunParam.getAccessKeySecret();

        DDNSParams.Ipv4 ipv4Param = ddnsParams.getIpv4();
        String[] ipv4DomainArr = ipv4Param.getDomains();
        if (ipv4DomainArr.length > 0) {
            String[] ipv4AddrArr = ipv4Param.getAddress();
            for (String getIpAddr : ipv4AddrArr) {
                String ipv4Str = HttpClient.doGet(getIpAddr);
                // 正则判断是IPV4地址则直接跳出循环
                break;
            }
        }

        DDNSParams.Ipv6 ipv6Param = ddnsParams.getIpv6();
        String[] ipv6DomainArr = ipv6Param.getDomains();
        if (ipv6DomainArr.length > 0) {
            String[] ipv6AddrArr = ipv6Param.getAddress();
            for (String getIpAddr : ipv6AddrArr) {
                String ipv6Str = HttpClient.doGet(getIpAddr);
                // 正则判断
            }
        }

    }

    private void tencent() {
        DDNSParams.TencentCloud tencentParam = ddnsParams.getTencentCloud();
        String secretId = tencentParam.getSecretId();
        String secretKey = tencentParam.getSecretKey();

        DDNSParams.Ipv4 ipv4Param = ddnsParams.getIpv4();
        String[] ipv4DomainArr = ipv4Param.getDomains();
        if (ipv4DomainArr.length > 0) {
            String[] ipv4AddrArr = ipv4Param.getAddress();
            for (String getIpAddr : ipv4AddrArr) {
                String ipv4Str = HttpClient.doGet(getIpAddr);
                // 正则判断是IPV4地址则直接跳出循环
                break;
            }
        }

        DDNSParams.Ipv6 ipv6Param = ddnsParams.getIpv6();
        String[] ipv6DomainArr = ipv6Param.getDomains();
        if (ipv6DomainArr.length > 0) {
            String[] ipv6AddrArr = ipv6Param.getAddress();
            for (String getIpAddr : ipv6AddrArr) {
                String ipv6Str = HttpClient.doGet(getIpAddr);
                // 正则判断
            }
        }

    }

}
